# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2024/1/18 14:06

from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.user_product_computing_locator import ProductComputingLocator
from Case.python_ui.PageLocate.user_product_computing_ros_locator import ProductComputingLocatorRos
from Case.python_ui.Test_data.menu_name_data import MenuName
import random
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.Pageobject.admin_product_managent_pages import ProductManagementPages

import time


class ProductComputingRos(Basepage):

    # 根据tab菜单名称进行点击
    def click_my_resource_tab(self, tab_name):
        doc = '根据my resource页的tab菜单名字进行点击进入'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.my_resource_tab_list, doc)
        my_resource_tab_list = self.get_elements(ProductComputingLocator.my_resource_tab_list, doc)
        for i in my_resource_tab_list:
            print('*****:', i.text, type(i))
            if i.text == tab_name:
                print('++++:', i.text)
                i.click()
        time.sleep(2)

    # 根据产品名称进行点击
    def get_product_name_to_click(self, product_name):
        doc = '根据产品名称然后点击'
        prod_list = self.get_elements(ProductComputingLocator.my_resource_product_list, doc)
        time.sleep(2)
        for prod in prod_list:
            print('+++++:', prod.text)
            if prod.text == product_name:
                prod.click()
                break
        time.sleep(5)

    # tab页根据产品名称进行点击进入控制台
    def click_product_by_name(self, product_name):
        doc = 'tab页根据产品名称进行点击进入控制台'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.move_computing_create_new, doc)
        self.move_mouse_to_element(ProductComputingLocator.move_computing_create_new)
        time.sleep(2)
        self.get_product_name_to_click(product_name)
        time.sleep(2)


    # 进入Resource Orchestration Service管理控制页
    def enter_resource_orchestration_service_page(self, cmdopt):
        doc = 'Resource Orchestration Service管理控制页'
        self.click_my_resource_tab(MenuName.compute_name[cmdopt])
        self.click_product_by_name('Resource Orchestration Service')
        for i in range(3):
            try:
                self.wait_elevisble(ProductComputingLocatorRos.iframe_resource_orchestration_service, doc)
                self.driver.switch_to.frame(self.get_element(ProductComputingLocatorRos.iframe_resource_orchestration_service, doc))
                self.wait_elevisble(ProductComputingLocatorRos.if_enter_resource_orchestration_service_page, doc)
                if self.get_element(ProductComputingLocatorRos.if_enter_resource_orchestration_service_page, doc).text == 'Stack':
                    break
            except:
                continue
        #print("++++:",self.get_element(ProductComputingLocatorRos.if_stack_id, doc).text)
        time.sleep(10)
        print("+++:", self.get_element(ProductComputingLocatorRos.if_enter_resource_orchestration_service_page, doc).text)
        assert self.get_element(ProductComputingLocatorRos.if_enter_resource_orchestration_service_page, doc).text == 'Stack'
        #assert self.get_element(ProductComputingLocatorRos.if_stack_id, doc).text == 'Stack ID/Name'
        assert self.get_element(ProductComputingLocatorRos.if_stack_org, doc).text == 'Organization'
        #assert self.get_element(ProductComputingLocator.if_stack_resset, doc).text == 'Resource S...'
        assert 'Resource' in self.get_element(ProductComputingLocatorRos.if_stack_resset, doc).text
        assert self.get_element(ProductComputingLocatorRos.if_stack_status, doc).text == 'Status'
        #assert self.get_element(ProductComputingLocator.if_stack_rollback, doc).text == 'Rollback on Fa...'
        assert 'Rollback' in self.get_element(ProductComputingLocatorRos.if_stack_rollback, doc).text
        #assert self.get_element(ProductComputingLocator.if_stack_timeout, doc).text == 'Timeo...'
        assert 'Time' in self.get_element(ProductComputingLocatorRos.if_stack_timeout, doc).text
        #assert self.get_element(ProductComputingLocator.if_stack_status_desc, doc).text == 'Status descrip...'
        assert 'Status' in self.get_element(ProductComputingLocatorRos.if_stack_status_desc, doc).text
        assert self.get_element(ProductComputingLocatorRos.if_stack_created, doc).text == 'Created At'
        assert self.get_element(ProductComputingLocatorRos.if_stack_updated, doc).text == 'Updated At'
        assert self.get_element(ProductComputingLocatorRos.if_stack_actions, doc).text == 'Actions'
        time.sleep(5)

    # 进入Resource Orchestration Service管理控制页不断言
    def enter_resource_orchestration_service_page_noassert(self, cmdopt):
        doc = 'Resource Orchestration Service管理控制页'
        self.click_my_resource_tab(MenuName.compute_name[cmdopt])
        self.click_product_by_name('Resource Orchestration Service')
        for i in range(3):
            try:
                self.wait_elevisble(ProductComputingLocatorRos.iframe_resource_orchestration_service, doc)
                self.driver.switch_to.frame(self.get_element(ProductComputingLocatorRos.iframe_resource_orchestration_service, doc))
                self.wait_elevisble(ProductComputingLocatorRos.if_enter_resource_orchestration_service_page, doc)
                if self.get_element(ProductComputingLocatorRos.if_enter_resource_orchestration_service_page, doc).text == 'Stack':
                    break
            except:
                continue
        #print("++++in:", self.get_element(ProductComputingLocator.input_template, doc).text)
        print("++++cl:", self.get_element(ProductComputingLocatorRos.click_create_stack, doc).text)
        time.sleep(5)

    #创建一个stacks
    def create_stack_name(self):
        doc = '创建一个stacks'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocatorRos.click_create_stack, doc)
        self.click_element(ProductComputingLocatorRos.click_create_stack, doc)
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocatorRos.input_resource_set, doc)
        self.move_mouse_to_click_element(ProductComputingLocatorRos.input_resource_set, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocatorRos.select_resource_set_value, doc)
        self.move_mouse_to_click_element(ProductComputingLocatorRos.select_resource_set_value, doc)
        #time.sleep(2)
        #self.wait_elevisble(ProductComputingLocator.select_template, doc)
        #self.move_mouse_to_click_element(ProductComputingLocator.select_template, doc)
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocatorRos.input_template, doc)
        dis = self.is_element_disabled(ProductComputingLocatorRos.input_template)
        print(dis)
        self.scroll_bar_operation(ProductComputingLocatorRos.input_template)
        time.sleep(2)
        keys = '{"ROSTemplateFormatVersion": "2015-09-01"}'
        #self.move_mouse_to_click_element(ProductComputingLocator.input_template, doc)
        print("input:", self.get_element(ProductComputingLocatorRos.input_template, doc).text)
        self.send_keys(ProductComputingLocatorRos.input_template, keys, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocatorRos.click_next, doc)
        self.move_mouse_to_click_element(ProductComputingLocatorRos.click_next, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocatorRos.input_name, doc)
        random_num = random.randint(1, 100000)
        stackname = 'Ydp'+str(random_num)
        self.send_keys(ProductComputingLocatorRos.input_name, stackname, doc)
        self.wait_elevisble(ProductComputingLocatorRos.second_click_next, doc)
        self.move_mouse_to_click_element(ProductComputingLocatorRos.second_click_next, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocatorRos.third_click_next, doc)
        self.move_mouse_to_click_element(ProductComputingLocatorRos.third_click_next, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocatorRos.click_create_succ, doc)
        self.move_mouse_to_click_element(ProductComputingLocatorRos.click_create_succ, doc)
        time.sleep(5)


    #删除stacks
    def delete_stack_name(self):
        doc = '删除一个stacks'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocatorRos.click_delete, doc)
        self.click_element(ProductComputingLocatorRos.click_delete, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocatorRos.ensure_delete, doc)
        self.move_mouse_to_click_element(ProductComputingLocatorRos.ensure_delete, doc)






